package com.kaka.bus.engine.home.info.amountlist;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.kaka.bus.R;
import com.kaka.bus.adapter.AmountListAdapter;
import com.kaka.bus.base.BaseActivity;
import com.kaka.bus.bean.HttpWrapper;
import com.kaka.bus.bean.OrderBean;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.di.component.DaggerLoanComponent;
import com.kaka.bus.engine.home.main.h5.H5Activity;
import com.kaka.bus.util.ToastUtil;
import com.kaka.bus.util.U;
import com.kaka.bus.view.MutipleLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class AmountActivity extends BaseActivity<AmountPresenter> implements AmountContract.View {
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.main_recy)
    RecyclerView mRecy;

    @BindView(R.id.borrowlist_smart)
    SmartRefreshLayout mBorrowlistSmart;
    @BindView(R.id.borrowlist_muti)
    MutipleLayout mBorrowlistMuti;

    private int mPage = 1;
    private int mPageSize = 20;


    AmountListAdapter amountListAdapter;
    List<OrderBean> mDatas;
    public Intent mIntent;

    @Override
    protected void initView() {
        super.initView();
        mToolbarName.setText("我的账单");
        mToolbarBack.setOnClickListener(v -> finish());
        mDatas = new ArrayList<>();
        mRecy.setLayoutManager(new LinearLayoutManager(this));
        amountListAdapter = new AmountListAdapter(this, R.layout.item_info_amount, mDatas);
        mRecy.setAdapter(amountListAdapter);
        amountListAdapter.bindToRecyclerView(mRecy);
        mBorrowlistMuti.setEmptyText("您当前暂无账单记录!");
        mBorrowlistMuti.setEmptyImage(R.drawable.empty_view_kongtai);
        mBorrowlistMuti.setStatus(MutipleLayout.Loading);

    }

    @Override
    protected void initInject() {
        DaggerLoanComponent.builder().activityComponent(getActivityComponent()).build().inject(this);
    }


    public void getData() {
        mPage = 1;
        mDatas.clear();
        mPresenter.getAmountList(getUserId(), getToken(), 1, mPageSize, Constant.PLATFORM, U.getAppInfo(this).getVersionName());
    }

    @Override
    protected void initData() {
        super.initData();
        mIntent = new Intent();
        mPresenter.getAmountList(getUserId(), getToken(), 1, mPageSize, Constant.PLATFORM, U.getAppInfo(this).getVersionName());

    }

    @Override
    protected void initListener() {
        super.initListener();
        mBorrowlistSmart.setOnRefreshListener(refreshlayout -> {
            getData();
        });

        mBorrowlistSmart.setOnLoadmoreListener(refreshlayout -> {
            mPresenter.loadMore(getUserId(), getToken(), ++mPage, mPageSize, Constant.PLATFORM, U.getAppInfo(this).getVersionName());
        });
        amountListAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {


            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.tv_find) {
                    mIntent.setClass(AmountActivity.this, H5Activity.class);
                    mIntent.putExtra(H5Activity.URL, mDatas.get(position).getDetailUrl());
                    startActivity(mIntent);
                }
            }
        });

        mBorrowlistMuti.setOnReloadListener(v -> {
            getData();
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_amount;
    }

    @Override
    public void onError() {
        mPage--;
        mBorrowlistSmart.finishLoadmore(0);
        mBorrowlistMuti.setStatus(MutipleLayout.Error);
    }

    @Override
    public void onResultStart(HttpWrapper<List<OrderBean>> data) {
        mBorrowlistSmart.finishRefresh(0);
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            if (data.data != null && data.data.size() > 0) {
                amountListAdapter.addData(data.data);
                mBorrowlistMuti.setStatus(MutipleLayout.Success);
            } else {
                mBorrowlistMuti.setStatus(MutipleLayout.Empty);
            }
        } else if (Constant.ERROR_CODE.equals(data.getCode())) {
            showMessage(data.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(data.code)) {
            U.tokenOut(this);
        }
    }

    @Override
    public void onResultLoadMore(HttpWrapper<List<OrderBean>> o) {
        mBorrowlistSmart.finishLoadmore(0);
        if (Constant.SUCCESS_CODE.equals(o.getCode())) {
            if (o.data != null && o.data.size() > 0) {
                amountListAdapter.addData(o.data);
            } else {
                showMessage("没有更多数据了");
                mPage--;
            }
        } else if (Constant.ERROR_CODE.equals(o.getCode())) {
            showMessage(o.getInfo());
            onError();
        } else if (Constant.TOKEN_TIMEOUT.equals(o.code)) {
            U.tokenOut(this);
        }

    }

    @Override
    public void refresh(HttpWrapper<List<OrderBean>> data) {
        if (Constant.SUCCESS_CODE.equals(data.code)) {
            getData();
        } else {
            ToastUtil.showShort(this, "取消失败！");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        getData();
    }

    @Override
    protected boolean needFullScreen() {
        return true;
    }

    //    @Override
//    protected void onRestart() {
//        getData();
//    }

}
